#!/bin/bash
# 企业版v3.6.4
# 部署MySQL （双主） redis 后
source comm.cfg

color_text() {
    local color=$1
    local text=$2
    echo -e "\e[${color}m${text}\e[0m"
}

error() {
    color_text "31" "$1"
}

info() {
    color_text "32" "$1"
}

installjms01() {
    # 配置jumpserver
    tar xf ${INSTALLDIR}/jumpserver-offline-release-${JMSVERSION}-amd64.tar.gz
    if [ $? -ne 0 ];then
        echo "请添加安装包到当前目录"
        exit
    fi
    # 创建jms用户名密码，只在一台上创建用户名密码就行
    sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "create database jumpserver default charset 'utf8';"
    sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "create user 'jumpserver'@'%' identified by '${DB_PASSWORD}';"
    sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "grant all on jumpserver.* to 'jumpserver'@'%';"
    sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "flush privileges;"

    cp ${INSTALLDIR}/config-example.txt ${INSTALLDIR}/jumpserver-offline-release-${JMSVERSION}-amd64
    cd ${INSTALLDIR}/jumpserver-offline-release-${JMSVERSION}-amd64
    # 确认数据目录是否存在
    datadir=`grep VOLUME_DIR config-example.txt | awk -F '=' '{print $2}'`
    if [ ! -d $datadir ];then
        mkdir -p $datadir
    fi
    # 每台机器修改MySQL redis的HOST port password 
    sed -i "s/DB_HOST=.*/DB_HOST=$DB_HOST/" config-example.txt
    if [ -n $DB_PORT ];then
        sed -i "s/^DB_PORT=.*/DB_PORT=$DB_PORT/" config-example.txt
    fi
    sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=$DB_PASSWORD/" config-example.txt
    sed -i "s|^REDIS_SENTINEL_HOSTS=.*|REDIS_SENTINEL_HOSTS=$REDIS_SENTINEL_HOSTS|" config-example.txt
    #sed -i "s/^REDIS_SENTINEL_PASSWORD=.*/REDIS_SENTINEL_PASSWORD=$REDIS_SENTINEL_PASSWORD/" config-example.txt
    sed -i "s/REDIS_PASSWORD=.*/REDIS_PASSWORD=$REDIS_PASSWORD/" config-example.txt
    # 修改HOSTNAME
    sed -i "s/SERVER_HOSTNAME=.*/SERVER_HOSTNAME=$HOSTNAME/" config-example.txt
    # 安装并启动jms
    # 安装
    ./jmsctl.sh install
    # 启动
    ./jmsctl.sh start
    # 检查是否启动成功
    port_list="81 2222 3389"
    for i in ${port_list} ; do
        if [[ ! `ss -nlpt | grep -w :${i}` ]] ; then
            echo "$i 异常，jumpserver启动失败"
        else
            echo "$i 正常，jumpserver启动成功"
        fi
    done
}

installjms02() {
    # 配置jumpserver
    tar xf ${INSTALLDIR}/jumpserver-offline-release-${JMSVERSION}-amd64.tar.gz
    if [ $? -ne 0 ];then
        echo "请添加安装包到当前目录"
        exit
    fi
    # 更新jumpserver用户权限
    sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "create user 'jumpserver'@'%' identified by '${DB_PASSWORD}';"
    sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "grant all on jumpserver.* to 'jumpserver'@'%';"
    sudo mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "flush privileges;"

    cp ${INSTALLDIR}/config-example.txt ${INSTALLDIR}/jumpserver-offline-release-${JMSVERSION}-amd64
    cd ${INSTALLDIR}/jumpserver-offline-release-${JMSVERSION}-amd64
    # 确认数据目录是否存在
    datadir=`grep VOLUME_DIR config-example.txt | awk -F '=' '{print $2}'`
    if [ ! -d $datadir ];then
        mkdir -p $datadir
    fi
    # 每台机器修改MySQL redis的HOST port password 
    sed -i "s/DB_HOST=.*/DB_HOST=$DB_HOST/" config-example.txt
    if [ -n $DB_PORT ];then
        sed -i "s/^DB_PORT=.*/DB_PORT=$DB_PORT/" config-example.txt
    fi
    sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=$DB_PASSWORD/" config-example.txt
    sed -i "s|^REDIS_SENTINEL_HOSTS=.*|REDIS_SENTINEL_HOSTS=$REDIS_SENTINEL_HOSTS|" config-example.txt
    #sed -i "s/^REDIS_SENTINEL_PASSWORD=.*/REDIS_SENTINEL_PASSWORD=$REDIS_SENTINEL_PASSWORD/" config-example.txt
    sed -i "s/REDIS_PASSWORD=.*/REDIS_PASSWORD=$REDIS_PASSWORD/" config-example.txt
    # 修改HOSTNAME
    sed -i "s/SERVER_HOSTNAME=.*/SERVER_HOSTNAME=$HOSTNAME/" config-example.txt
    # 启动
    jmsready ${JMS01}
    ./jmsctl.sh install
    ./jmsctl.sh start
    # 检查当前jms是否启动成功
    port_list="81 2222 3389"
    for i in ${port_list} ; do
        if [[ ! `ss -nlpt | grep -w :${i}` ]] ; then
            echo "$i 异常，jumpserver启动失败"
        else
            echo "$i 正常，jumpserver启动成功"
        fi
    done
}
# 等待其他节点初始化完成
jmsready() {
    for i in $(seq 1 10)
    do
        nc -zv $1 81 &> /dev/null && nc -zv $1 2222 &> /dev/null && nc -zv $1 3389 &> /dev/null
        if [ $? -ne 0 ];then
            error "Waiting Ready......"
            sleep 30
        else
            info "$1 jumpserver Is Ready"
            return 0
        fi
    done
    error "Waiting jumpserver Timeout"
    return 1
}

jmsinstall() {
    ip a |grep ${JMS01}
    if [ $? -eq 0 ];then
        installjms01
    else
        installjms02
    fi
}

jmsinstall
